<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :inline="true">
      <el-form-item label="对账单号" prop="reconDocsNo">
        <el-input v-model="form.reconDocsNo" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="对账客户" prop="customerName">
        <el-input v-model="form.customerName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="单据类型" prop="reconDocsType">
        <el-select v-model="form.reconDocsType" filterable clearable>
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" filterable clearable>
          <el-option
            v-for="item in dict.type.ims_recon_status"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="对账日期" prop="time">
        <el-date-picker v-model="form.time" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
                        value-format="yyyy-MM-dd" clearable></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="success" v-hasPermi="['ims:payCheck2:export']" plain icon="el-icon-download" @click="handleExport">
      导出
    </el-button>
    <el-table
      v-loading="loading"
      :data="list"
    >
      <el-table-column type="index" label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="reconDocsNo" label="对账单号" align="center"  min-width="150">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateRow(1,scope.row)">{{ scope.row.reconDocsNo }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="reconTime" label="对账日期" align="center"/>
      <el-table-column prop="reconDocsType" label="单据类型" align="center">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.reconDocsType==10">自动生成</el-tag>
          <el-tag type="success" v-if="scope.row.reconDocsType==20">手动生成</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="paymentType" label="结算方式" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_payment_type" :value="scope.row.paymentType"/>
        </template>
      </el-table-column>
      <el-table-column prop="customerName" label="对账客户" align="center"/>
      <el-table-column prop="customerType" label="客户类型" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_customer_type" :value="scope.row.customerType"/>
        </template>
      </el-table-column>
      <el-table-column prop="income" label="收款金额(元)" align="center"/>
      <el-table-column prop="status" label="状态" align="center" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ims_recon_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column prop="reviewUserName" label="审核人" align="center" width="150"/>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="updateRow(1,scope.row)"
          >详情
          </el-button>
          <el-button
            type="text"
            v-if="scope.row.status!=70"
            v-hasPermi="['ims:payCheck:delete']"
            @click="updateRow(3,scope.row)"
          >删除
          </el-button>
          <el-button
            v-hasPermi="['ims:payCheck2:confirm']"
            v-if="scope.row.status==30"
            type="text"
            @click="updateRow(4,scope.row)"
          >确认
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="form.pageNum"
      :limit.sync="form.pageSize"
      @pagination="getList"
    />
    <updateDialog ref="updateDialog" @ok="handleQuery"></updateDialog>
  </div>
</template>

<script>
import updateDialog from "./updateDialog";
import {reloadPayCheck, listPayCheck, deletePayCheck} from "../../../../api/payMng";
import {formatDay} from "../../../../utils";

export default {
  dicts: ['ims_recon_status','ims_payment_type','sys_customer_type'],
  components: {updateDialog},
  data() {
    return {
      loading: false,
      list: [],
      form: {
        reconDocsNo: "",
        reconDocsType: "",
        customerName: "",
        status: "",
        time:[],
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      typeList: [
        {label: "自动生成", value: '10'},
        {label: "手动生成", value: '20'}
      ]
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.handleQuery()
    },
    handleQuery() {
      this.form.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('form')
      this.handleQuery()
    },
    handleExport() {
      this.download('/ims/recon/export', {
        ...this.form
      }, `对账单_${formatDay(new Date())}.xlsx`)
    },
    reload() {
      this.$modal.confirm('刷新之后会重新生成昨天的对账单，请谨慎操作！').then(e=>{
        this.loading = true
        reloadPayCheck({}).then(res => {
          this.handleQuery()
          this.$message.success('刷新成功')
        }).finally(e => {
          this.loading = false
        })
      })
    },
    getList() {
      this.loading = false
      if(this.form.time&&this.form.time.length){
        this.form.startTime=this.form.time[0]
        this.form.endTime=this.form.time[1]
      }else{
        this.form.startTime=''
        this.form.endTime=''
      }
      listPayCheck(this.form).then(res => {
        this.list = res.rows
        this.total = res.total
      }).finally(e => {
        this.loading = false
      })
    },
    updateRow(flag, row) {
      let form = JSON.parse(JSON.stringify(row))
      if (flag == 3) {
        this.$modal.confirm('是否确定删除').then(res => {
          deletePayCheck({id: form.id}).then(res => {
            this.handleQuery()
            this.$message.success('删除成功')
          })
        })
      } else {
        this.$refs.updateDialog.show(flag, form)
      }
    }
  }
}
</script>
